﻿using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.ComponentModel.Composition.Hosting;
using System.IO;
using System.Reflection;
using System.Text;
using System.Windows.Forms;
using Moonlite.Composition;
using Moonlite.ExtensionApi;
using Moonlite.Platform.General;
namespace Moonlite
{
    public static class Program
    {
        public static CompositionContainer Container { get; private set; }
        public static RefreshCatalog RefreshCatalog { get; private set; }
        public static DirectoryCatalog DirectoryCatalog { get; private set; }

        private static void Compose()
        {
            DirectoryCatalog = new DirectoryCatalog(Application.StartupPath);
            AssemblyCatalog assemblyCatalog = new AssemblyCatalog(Assembly.GetExecutingAssembly());
            AggregateCatalog aggregateCatalog = new AggregateCatalog();
            aggregateCatalog.Catalogs.Add(DirectoryCatalog);
            aggregateCatalog.Catalogs.Add(assemblyCatalog);
            Container = new CompositionContainer(aggregateCatalog);
            RefreshCatalog = new RefreshCatalog(DirectoryCatalog);
            Container.ComposeParts(RefreshCatalog);
            Application.Run(Container.GetExportedValue<Shell>());
        }

        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        public static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            ExtensionManagerForm.PartsToBeRemoved = new SerializableDictionary();
            try
            {
                string settingsPath = Global.GetAppDataSettingsFolder();
                DirectoryHelper.CreateFolderPath(settingsPath);
                if (File.Exists(settingsPath + "\\RemovedExtensions.xml") == true)
                {
                    ExtensionManagerForm.PartsToBeRemoved.ReadXml(settingsPath + "\\RemovedExtensions.xml");
                }

                foreach (KeyValuePair<string, bool> valuePair in ExtensionManagerForm.PartsToBeRemoved)
                {
                    if (valuePair.Value != true)
                    {
                        continue;
                    }

                    try
                    {
                        File.Delete(valuePair.Key);
                    }
                    catch (IOException exception)
                    {
                        Logger.Log += exception.Message + Environment.NewLine;
                    }
                }

                ExtensionManagerForm.PartsToBeRemoved.Clear();
                ExtensionManagerForm.PartsToBeRemoved.WriteXml(settingsPath + "\\RemovedExtensions.xml");
            }
            catch (Exception exception)
            {
                MessageBox.Show(exception.Message);
            }

            Compose();
        }
    }
}
